// 验证工具

// 手机号验证
export const validatePhone = (phone) => {
  const reg = /^1[3-9]\d{9}$/
  return reg.test(phone)
}

// 邮箱验证
export const validateEmail = (email) => {
  const reg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
  return reg.test(email)
}

// 身份证验证
export const validateIdCard = (idCard) => {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  return reg.test(idCard)
}

// 密码验证（至少8位，包含数字和字母）
export const validatePassword = (password) => {
  const reg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/
  return reg.test(password)
}

// 验证码验证（6位数字）
export const validateCode = (code) => {
  const reg = /^\d{6}$/
  return reg.test(code)
}

// 金额验证（两位小数）
export const validateMoney = (money) => {
  const reg = /^[0-9]+(\.[0-9]{1,2})?$/
  return reg.test(money)
}

// 正整数验证
export const validateInteger = (num) => {
  const reg = /^[1-9]\d*$/
  return reg.test(num)
}

// 非空验证
export const validateRequired = (value) => {
  if (value === null || value === undefined) return false
  if (typeof value === 'string') return value.trim().length > 0
  if (Array.isArray(value)) return value.length > 0
  return true
}

// 长度验证
export const validateLength = (value, min, max) => {
  if (!value) return false
  const length = typeof value === 'string' ? value.length : value.toString().length
  return length >= min && length <= max
}

// 范围验证
export const validateRange = (value, min, max) => {
  const num = parseFloat(value)
  return !isNaN(num) && num >= min && num <= max
}

// 表单验证
export const validateForm = (rules, formData) => {
  const errors = {}
  
  for (const field in rules) {
    const value = formData[field]
    const fieldRules = rules[field]
    
    for (const rule of fieldRules) {
      const { type, message, ...params } = rule
      
      let isValid = true
      
      switch (type) {
        case 'required':
          isValid = validateRequired(value)
          break
        case 'phone':
          isValid = validatePhone(value)
          break
        case 'email':
          isValid = validateEmail(value)
          break
        case 'password':
          isValid = validatePassword(value)
          break
        case 'code':
          isValid = validateCode(value)
          break
        case 'money':
          isValid = validateMoney(value)
          break
        case 'integer':
          isValid = validateInteger(value)
          break
        case 'length':
          isValid = validateLength(value, params.min, params.max)
          break
        case 'range':
          isValid = validateRange(value, params.min, params.max)
          break
        case 'custom':
          isValid = params.validator ? params.validator(value) : true
          break
      }
      
      if (!isValid) {
        errors[field] = message
        break
      }
    }
  }
  
  return {
    isValid: Object.keys(errors).length === 0,
    errors
  }
}

// 显示验证错误
export const showValidationError = (errors) => {
  const firstError = Object.values(errors)[0]
  if (firstError) {
    uni.showToast({
      title: firstError,
      icon: 'none'
    })
  }
} 